/**
 * Javascript function to get Seagate workweek
 * Please keep these lines on when you copy the source
 * Made by: Phuwarin Supawarapong, 14 July 2005
 *
 * Implementation: you must put this file in the head secion of the html file
 * <HEAD>
 * <script src="./scripts/seagateww.js"></script>
 * </HEAD>
 * <BODY>
 *      <script>
 *      <!--
 *         document.write(SeagateWW())
 *      //-->
 *      </script>
 * </BODY>
 */

var monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var shortMonthName =	new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var firstDayOfWW1 = new Date(2001, 5, 30); // first day of ww01 year 2002
                                                                      
function SeagateWW()
{
	return getSeagateWW(new Date()); // work with Julian date
}

/**
 * @return number of days between any two dates
 */
function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

/**
 * Get a Seagate work week
 * @return with format FY9999WW99
 */
function getSeagateWW(aDate) {

  if (getDaysBetween(firstDayOfWW1, aDate) >= 0) {
  while (getDaysBetween(firstDayOfWW1, aDate) >= 364) {
    firstDayOfWW1 = advanceDays(firstDayOfWW1, 364);
    if ((firstDayOfWW1.getDate() < 28) && (firstDayOfWW1.getMonth() == 5)) {
      firstDayOfWW1 = advanceDays(firstDayOfWW1, 7);
    }
  }
  }

  if (getDaysBetween(firstDayOfWW1, aDate) < 0) {
  while (getDaysBetween(firstDayOfWW1, aDate) < 0) {
    firstDayOfWW1 = advanceDays(firstDayOfWW1, -364);
    if ((firstDayOfWW1.getDate() > 4) && (firstDayOfWW1.getMonth() == 6)) {
      firstDayOfWW1 = advanceDays(firstDayOfWW1, -7);
    }
  }
  }
  
  dayDif = getDaysBetween(firstDayOfWW1, aDate);
  wkBuf = Math.floor(dayDif / 7) + 1;
  if (wkBuf < 10) wkBuf = "0" + wkBuf;
  
  basedYear = firstDayOfWW1.getFullYear() + 1;

  return "FY" + basedYear + "WW" + wkBuf;
}

/**
 * Algorithm used:
 * From Klaus Tondering's Calendar document (The Authority/Guru)
 * http://www.tondering.dk/claus/calendar.html
 * 
 * @return the week number
 */
function getWeekNumber(date) {

   var year = date.getFullYear();
   var month = date.getMonth() + 1;
   var day = date.getDate();

   a = Math.floor((14-month) / 12);
   y = year + 4800 - a;
   m = month + 12 * a - 3;
   b = Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
   J = day + Math.floor((153 * m + 2) / 5) + 365 * y + b - 32045;
   d4 = (((J + 31741 - (J % 7)) % 146097) % 36524) % 1461;
   L = Math.floor(d4 / 1460);
   d1 = ((d4 - L) % 365) + L;
   week = Math.floor(d1/7) + 1;

   return week;
}

/**
 * Gets the weekday
 * @return the weekday ({0=SUNDAY}, ..., {7=SATURDAY})
 */
function getDayOfWeek(date) { 
	return (toJulianDate(date) + 1) % 7 + 1; 
}

/**
 * @return the number of days between fromDate and toDate parameter
 *  (> 0 if toDate comes after fromDate)
 */
function getDaysBetween(fromDate, toDate) {
   return toJulianDate(toDate) - toJulianDate(fromDate);
}

/**
 * Advances this day by n days. For example.
 * d.advance(30) adds thirdy days to d
 *
 * @param n the number of days by which to change this day (can be < 0)
 */
function advanceDays(aDate, n) {
	return getJulianDate(toJulianDate(aDate) + n);
}

/**
 * @return The Julian day number that begins at noon of this day
 * Positive year signifies A.D., negative year B.C.
 * Remember that the year after 1 B.C. was 1 A.D.
 *
 * A convenient reference point is that May 23, 1968 noon
 * is Julian day 2440000.
 *
 * Julian day 0 is a Monday.
 *
 * This algorithm is from Press et al., Numerical Recipes
 * in C, 2nd ed., Cambridge University Press 1992
 */
function toJulianDate(date)
{
  var tjdYear = date.getFullYear();
  var tjdMonth = date.getMonth()+1;
  var tjdDay = date.getDate();
  jy = tjdYear;
  if (tjdYear < 0) jy++;
  jm = tjdMonth;
  if (tjdMonth > 2) jm++;
  else {
    jy--;
    jm += 13;
  }
  
  tjul = Math.floor((Math.floor(365.25 * jy)
       + Math.floor(30.6001*jm) + tjdDay + 1720995.0));

  IGREG = 15 + 31*(10+12*1582);
  // Gregorian Calendar adopted Oct. 15, 1582

  if (tjdDay + 31 * (tjdMonth + 12 * tjdYear) >= IGREG) {
    // change over to Gregorian calendar
    ja = Math.floor(0.01 * jy);
    tjul += 2 - ja + Math.floor(0.25 * ja);
  }
  
  return tjul;
}

/**
 * Converts a Julian day to a calendar date
 *
 * This algorithm is from Press et al., Numerical Recipes
 * in C, 2nd ed., Cambridge University Press 1992
 *
 * @param juDate the Julian date
 */
function getJulianDate(juDate) {
   ja = juDate;
   JGREG = 2299161;
   // the Julian date of the adoption of the Gregorian calendar

   if (juDate >= JGREG) {
     // cross-over to Gregorian Calendar produces this correction
     jalpha = Math.floor(((juDate - 1867216) - 0.25) / 36524.25);
     ja += 1 + jalpha - Math.floor(0.25 * jalpha);
   }
   jb = ja + 1524;
   jc = Math.floor(6680.0 + ((jb-2439870) - 122.1) / 365.25);
   jd = Math.floor(365 * jc + (0.25 * jc));
   je = Math.floor((jb - jd) / 30.6001);
   
   var day = jb - jd - Math.floor(30.6001 * je);
   var month = je - 1;
   if (month > 12) month -= 12;
   var year = jc - 4715;
   if (month > 2) --year;
   if (year <= 0) --year;

   return new Date(year, --month, day);
}

/**
 * Find a first day of week01 for a given year
 * @return with format of JDate
 */
function getFirstDayOfWeek01(fiscalYear) {
  aYear = firstDayOfWW1.getFullYear();
  while ((aYear + 1) < fiscalYear) {
	firstDayOfWW1 = advanceDays(firstDayOfWW1, 364);
    if ((firstDayOfWW1.getDate() < 28) && (firstDayOfWW1.getMonth() == 5)) {
      firstDayOfWW1 = advanceDays(firstDayOfWW1, 7);
    }
  }
  return firstDayOfWW1;
}

/* 
 * Get current date
 * @parameter format a string date format, default mm/dd/yyyy
 * @return current date specified format 
 */
function todayDate( format )
{
	var today = new Date()
	var month = today.getMonth()+1
	var year = today.getFullYear()
	var day = today.getDate()

	if (day<10) day = "0" + day
    if (month<10) month= "0" + month 
	
	if (format == "dd/mm/yyyy") {
	     return day + "/" + month + "/" + year;
	} else if (format == "dd-mmm-yyyy") {
         return day + "-" + shortMonthName[parseInt(month, 10) - 1] + "-" + year;
	} else if (format == "dd-mmmm-yyyy") {
	     return day + "-" + monthName[parseInt(month, 10) - 1] + "-" + year;
	} else if (format == "mmm dd, yyyy") {
	     return shortMonthName[parseInt(month, 10) - 1] + " " + day + ", "  + year;
	} else 
	     return month + "/" + day + "/" + year;
}